package com.frejoys.notify.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.config.ValueConfig;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.enums.EHuaJuan;
import com.frejoys.common.enums.ELoginType;
import com.frejoys.common.enums.ESite;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CheckUtil;
import com.frejoys.common.util.TimeUtil;
import com.frejoys.common.vo.black.TmpVo;
import com.frejoys.dao.entity.User;
import com.frejoys.dao.entity.huajuan.HuajuanUser;
import com.frejoys.dao.mapper.HuajuanPushMapper;
import com.frejoys.dao.mapper.huajuan.HuajuanUserMapper;
import com.frejoys.service.TmpService;
import com.frejoys.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangzhicheng
 * @version 1.0
 * @date 2024/4/17 16:44
 */
@Tag(name = "花卷云", description = "花卷云")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixNotify)
@Authorization(Authority.Public)
@Slf4j
public class HuaJuanYunController {

    private final ValueConfig valueConfig;

    private final UserService userService;

    private final HuajuanPushMapper huajuanPushMapper;

    private final HuajuanUserMapper huajuanUserMapper;


    @Operation(summary = "接收花卷云推送")
    @PostMapping("/huajuan/push")
    public Result huajuanPush(@RequestBody String jsonData){

        if (StrUtil.isNotEmpty(jsonData)) {
            JSONObject json = JSONUtil.parseObj(jsonData);

            if (valueConfig.getHuajuanyunToken().equals(json.getStr("token"))) {

                String msgId = json.getStr("msg_id");
                // 消息已经接收过
                if (huajuanPushMapper.getIdByMsgId(msgId) != null) {
                    return Result.success();
                }

                String pushType = json.getStr("push_type");


                // 新用户注册
                if (EHuaJuan.memberRegister.getValue().equals(pushType)) {

                    String phone = json.getJSONObject("data").getStr("mobile");
                    Integer huajuanUserId = json.getJSONObject("data").getInt("id");

                    if (StrUtil.isNotEmpty(phone) && huajuanUserId != null) {
                        User u = userService.getSaveUserInfo(null, phone, ELoginType.phone.getValue(), ESite.app.getValue(), null, null);
                        if (u != null) {

                            HuajuanUser hjUser = huajuanUserMapper.getInfo(phone);
                            if (hjUser == null) {
                                huajuanUserMapper.saveInfo(phone, huajuanUserId, u.getId());
                            }

                        }
                    }

                }

                // 记录信息
                huajuanPushMapper.addInfo(msgId, pushType, jsonData, TimeUtil.unixTime());

            }

        }

        return Result.success();
    }

}
